﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.Xml;

namespace jihuang_Player
{
    public partial class Compilation : Form
    {
        public string projetPath;
        public XmlAttribute buildname;
        public XmlNodeList anlist;
        public XmlDocument buildxml;
        public XmlDocument anxml;
        public Compilation(string p)
        {
            projetPath=p;
            InitializeComponent();
            if(File.Exists("pythonPath"))
            {
                textBox1.Text = File.ReadAllText("pythonPath");
            } 
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "(python.exe)|python.exe";
                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    textBox1.Text = openFileDialog.FileName;
                    File.WriteAllText("pythonPath", openFileDialog.FileName);
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "zip files (*.zip)|*.zip";
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = saveFileDialog1.FileName;
            }
        }

        private void Compilation_Load(object sender, EventArgs e)
        {
            buildxml = new XmlDocument();
            Console.WriteLine(projetPath+@"\build.xml");
            buildxml.LoadXml(File.ReadAllText(projetPath+@"build.xml"));
            buildname = buildxml.GetElementsByTagName("Build")[0].Attributes["name"];
            textBox2.Text = buildname.Value;

            anxml = new XmlDocument();
            anxml.LoadXml(File.ReadAllText(projetPath + @"animation.xml"));
            anlist = anxml.GetElementsByTagName("anim");
            textBox3.Text = anlist[0].Attributes["root"].Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!File.Exists(textBox1.Text))
            {
                MessageBox.Show("python路径无效！");
                return;
            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("生成目录为空！");
                return;
            }
            buildname.Value = textBox2.Text;
            buildxml.Save(projetPath + @"build.xml");

            foreach(XmlNode node in anlist)
            {
                node.Attributes["root"].Value = textBox3.Text;
            }
            anxml.Save(projetPath + @"animation.xml");

            System.Diagnostics.ProcessStartInfo p = null;
            System.Diagnostics.Process Proc;
            string pythonscriptPath = System.Environment.CurrentDirectory + @"\pythonScript\buildanimation.py";
            p = new ProcessStartInfo(textBox1.Text, "\"" + pythonscriptPath + "\" " + projetPath + @"\a.png " + checkBox1.Checked + " " + checkBox2.Checked + " " + textBox4.Text);
            //p.CreateNoWindow = false;
            //p.RedirectStandardError = true;
            //p.RedirectStandardOutput = true;
            //p.UseShellExecute = false;
            //p.WorkingDirectory = exepath;//设置此外部程序所在windows目录 
            p.WindowStyle = ProcessWindowStyle.Normal;//在调用外部exe程序的时候，控制台窗口不弹出 //如果想获得当前路径为 //
            //string path = System.AppDomain.CurrentDomain.BaseDirectory; 
            Proc = System.Diagnostics.Process.Start(p);
            /*StreamReader read = Proc.StandardOutput;
            Console.WriteLine(read.ReadToEnd());
            StreamReader r = Proc.StandardError;
            Console.WriteLine(r.ReadToEnd());*/
        }
    }
}
